<?php
/**
 * 宣赫短信平台HTTP接口
 */
include_once 'sms.php';
class cxhsms extends csms{
	var $lastrecvtime = '';//上次接收到最近一条回复短信的回复时间, 用以迭代取回复短信.
	
	//服务器地址
	var $server = "http://www.lddxqf.cn:8080";//此是网通服务器
	
	function csms($u, $p){
		$this->user = $u;
		$this->passwd = $p;
	}
	
	function curmoney(){
		return $this->cursmscount();
	}
	
	function cursmscount(){
		$res = readfromurl("{$this->server}/softweb/login.asp?username={$this->user}&password={$this->passwd}");
		if("1|"==$res){//用户名密码错误
			return ESMS_UPERROR;
		}
		elseif("2|"==$res){
			return ESMS_USERCLOSE;
		}
		elseif(strpos($res, "3|")===0){//获得余额
			$money = substr($res, 2);
			return (int)$money;
		}
		else return ESMS_UNKNOWN;
	}
	
	function sendsms($fromnum = "", $tonum, $content, $sendtime){
		if(null==$content || ""==$content) return ESMS_SUC;
		if(null==$tonum || ""==$tonum || count($tonum)==0) return ESMS_EMPTYTO;
		
		//构造客户号码串
		if(is_array($tonum)){
			$tonumstr = '';
			foreach ($tonum as $to){
				$tonumstr .= "$to|";
			}
		}
		else{
			$tonumstr = $tonum;
		}
		
		$content = utf8togbk($content);//TODO 解析内容, 处理禁发关键字
		
		//TODO 处理时间
		$sendtime = "";
		
		//组合参数
		$params = array(
			 "username"=>$this->user
			,"password"=>$this->passwd
			,"smsnumber"=>$tonumstr
			,"smscontent"=>$content
			,"sendtime"=>$sendtime);
		
		//调用接口发送
		//readfromurl("{$this->server}/softweb/send.asp?username={$this->user}&password={$this->passwd}&smsnumber=$sendto&smscontent=$content&sendtime=$sendtime");
		$res = postrequest("{$this->server}/softweb/send.asp", $params);
		
		if("1|"==$res){//用户名密码错误
			return ESMS_UPERROR;
		}
		elseif("2|"==$res){
			return ESMS_USERCLOSE;
		}
		elseif("4|"==$res){
			return ESMS_NOMONEY;
		}
		elseif("5|"==$res){
			return ESMS_TONUMERR;
		}
		elseif("6|"==$res){
			return ESMS_CONTENTERR;
		}
		elseif("8|"==$res){
			return ESMS_MANYTONUM;
		}
		elseif("9|"==$res){//含有屏蔽字
			//TODO 处理禁发关键字
		}
		elseif(strpos($res, "7|")===0){//发送成功
			return ESMS_SUC;
		}
		else{
			return ESMS_UNKNOWN;
		}
	}
	
	function recvsms(){
		$starttime = $this->lastrecvtime;
		$endtime = "";
		
		$params = array(
			 "username"=>$this->user
			,"password"=>$this->passwd
			,"starttime"=>$starttime
			,"endtime"=>$endtime);
		$res = postrequest("{$this->server}/softweb/receive.asp", $params);
		$res = $res['result'];
		//echo "$res\n";
		if("1|"==$res){//用户名密码错误
			return ESMS_UPERROR;
		}
		elseif("2|"==$res){
			return ESMS_USERCLOSE;
		}
		elseif(@strpos($res, "4|")===0){//获得回复的短信
			$smses = array();
			$res = substr($res, 2);
			if(null==$res || ""==$res) return $smses;
			
			$smsesstr = explode("[$$]", $res);
			foreach ($smsesstr as $smsstr){
				if(null==$smsstr || ""==$smsstr) continue;
				
				$pos = strpos($smsstr, "*");
				$sms['num'] = substr($smsstr, 0, $pos);
				
				$smsstr = substr($smsstr, $pos+1);
				$pos = strpos($smsstr, "^");
				$sms['time'] = substr($smsstr, 0, $pos);
				
				$sms['content'] = gbktoutf8(substr($smsstr, $pos+1));
				
				$smses[] = $sms;
			}
			
			return $smses;
		}
		else return ESMS_UNKNOWN;
	}
}

//改密码
//echo readfromurl("http://www.lddxqf.cn:8080/softweb/changepassword.asp?username=$user&password=$passwd&newpassword="); 

//发送短信
$sendto = "18210913063";//多个:18618160032|13905142825|
$sendcontent = "哈!终于发出来啦:)请回复我,我会一直等你的";
$sendtime = "";//留空表示立刻发送, 否则格式为:2011/01/01 08:00:00
//echo 

//收短信softweb/receive.asp?username= & password = & startime = & endtime =
$starttime = "2011/01/01 08:00:00";
$endtime = "2011/10/01 08:00:00";
//$res = readfromurl("http://www.lddxqf.cn:8080/softweb/receive.asp?"
//	."username=$user&password=$passwd&starttime=".urlencode($starttime)
//	."&endtime=".urlencode($endtime), 1);
$res = 
$res = $res['result'];
//echo $res."\n";
//echo gbktoutf8($res);

$sms = new cxhsms("wg800053", "haha");
echo print_r($sms->recvsms(), 1);
?>